import 'package:asset_management/modules/components/utils/adapter.dart';
import 'package:flutter/material.dart';

class AButton extends StatefulWidget {
  const AButton({
    super.key,
    required this.child,
    this.icon,
    this.backgroundColor = Colors.transparent,
    this.forbidden = false,
    this.onPressed,
    this.isCircle = false,
    this.borderRadius,
    this.height,
    this.width,
    this.gap = 4,
    this.padding,
    this.border,
  });
  final Widget child;
  final Widget? icon;
  final bool forbidden;
  final VoidCallback? onPressed;
  final Color backgroundColor;
  final bool isCircle;
  final BorderRadiusGeometry? borderRadius;
  final double? height;
  final double? width;
  final double gap;
  final EdgeInsetsGeometry? padding;
  final Border? border;

  @override
  State<AButton> createState() => _AButtonState();
}

class _AButtonState extends State<AButton> {
  bool isHover = false;
  Widget _getChild() {
    return Container(
      padding: widget.padding ?? const EdgeInsets.all(8),
      color: widget.forbidden ? Colors.grey : widget.backgroundColor,
      child: Center(
        child: widget.icon != null
            ? Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  widget.icon!,
                  SizedBox(width: widget.gap),
                  widget.child,
                ],
              )
            : widget.child,
      ),
    );
  }

  Widget _getOuter() {
    return Stack(
      children: [
        Container(
          decoration: BoxDecoration(
            border:
                widget.border ?? Border.all(color: Colors.black.withAlpha(50)),
            borderRadius: widget.isCircle
                ? BorderRadius.circular(99999)
                : (widget.borderRadius ?? BorderRadius.circular(4)),
          ),
          child: MouseRegion(
            cursor: widget.forbidden
                ? SystemMouseCursors.forbidden
                : SystemMouseCursors.click,
            onEnter: (event) => setState(() {
              isHover = true;
            }),
            onExit: (event) => setState(() {
              isHover = false;
            }),
            child: GestureDetector(
              onTap: widget.forbidden ? null : widget.onPressed,
              child: _getChild(),
            ),
          ),
        ),
        IgnorePointer(
            child: Container(
          color: Colors.grey.withAlpha(isHover ? 50 : 0),
        )),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: SizedBox(
        height: widget.height,
        width: widget.width,
        child: widget.isCircle
            ? AspectRatio(
                aspectRatio: 1,
                child: ClipOval(
                  child: _getOuter(),
                ),
              )
            : ClipRRect(
                borderRadius: widget.borderRadius ?? BorderRadius.circular(4),
                child: _getOuter(),
              ),
      ),
    );
  }
}
